package com.study.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.study.model.pojo.Good;
import com.study.services.GoodsService;
import com.study.services.ITestService;
/**
 * excel文件导入导出
 * @author 历颂林
 *
 */
@Controller
public class TestController1 {
 
    @Autowired
    private ITestService testService;
    @Autowired
    GoodsService service;
    
    /**
     * 客户导入excel文件
     * @param file
     * @return
     */
    @PostMapping("/import")
    @ResponseBody
    public boolean addUser(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        System.out.println("文件来了吗？？？"+file);
        String fileName = file.getOriginalFilename();
        try {
             a = testService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  a;
    }
    
    @RequestMapping("/openimport")
	public ModelAndView getForm4(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/import");
		return mv;
	}
    
    /**
     * 产品导入
     * @param file
     * @return
     */
    @PostMapping("/goodimport")
    @ResponseBody
    public ModelAndView addGood(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        System.out.println("产品文件来了吗？？？"+file);
        String fileName = file.getOriginalFilename();
        try {
             a = testService.GoodsImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("导入结果："+a);
        ModelAndView mv = new ModelAndView("/goods/productList");
        return mv;
    }
    
    /**
     * 商品excel文件导出
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void downloadAllGoods(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<Good> list = service.findAll();
        String fileName = "goods"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = { "编号", "类别", "商品名称", "库存","单位","状态","价格"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Good g : list) {
        	 HSSFRow row1 = sheet.createRow(rowNum);
        	 row1.createCell(0).setCellValue(g.getGooId());
        	 row1.createCell(1).setCellValue(g.getCategory().getCatName());
        	 row1.createCell(2).setCellValue(g.getGooName());
        	 row1.createCell(3).setCellValue(g.getGooSpec());
        	 
        	 
        	 String dicName = null;
			String dicName2 = null;
			try {
				dicName = g.getDictionary1().getDicName();
				 dicName2 = g.getDictionary2().getDicName();
			} catch (Exception e) {
				//e.printStackTrace();
				System.out.println("有问题了");
			}
        	 
        	 
        	 row1.createCell(4).setCellValue(dicName);
			row1.createCell(5).setCellValue(dicName2);
        	 row1.createCell(6).setCellValue(g.getGooPrice().toString());
        	 rowNum++;
		}

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
